﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Collections;

namespace Zone
{
    public class CookieForEach
    {
        public static CookieCollection GetAllCookies(CookieContainer cc)
        {

            CookieCollection lstCookies = new CookieCollection();

            Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable",

                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |

                System.Reflection.BindingFlags.Instance, null, cc, new object[] { });

            foreach (object pathList in table.Values)
            {

                SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
                foreach (CookieCollection colCookies in lstCookieCol.Values)
                {
                    foreach (Cookie c in colCookies)
                    {
                        lstCookies.Add(c);
                    }
                    
                }
                   
            }
            return lstCookies;

        }
        public static string GetCookieText(CookieContainer cc )
        {
            StringBuilder sbc = new StringBuilder();

            CookieCollection cooklist = GetAllCookies(cc);

            foreach (Cookie cookie in cooklist)
            {

                sbc.AppendFormat("{0};{1};{2};{3};{4};{5}\r\n",

                    cookie.Domain, cookie.Name, cookie.Path, cookie.Port,

                    cookie.Secure.ToString(), cookie.Value);

            }
            return sbc.ToString();
            //DebugNew.DebugMethod(sbc.ToString() + "   " + System.Text.Encoding.Default);

            //FileStream fs = File.Create("d:\\chinarencookies.txt");

            //fs.Close();

            //File.WriteAllText("d:\\chinarencookies.txt", sbc.ToString(), System.Text.Encoding.Default); 

        }
       
    }
}
